package com.lb.jwitter.service;
 
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lb.jwitter.*;
import com.lb.jwitter.hibernate.*;
import com.lb.jwitter.util.ChumanoLogger;
import com.lb.jwitter.util.ConditionReport;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;


@Service("tbl_DKMH_HocVien_MonHoc_DuocDKService")
@Transactional
@WebService(endpointInterface = "com.lb.jwitter.service.Tbl_DKMH_HocVien_MonHoc_DuocDKService")
public class Tbl_DKMH_HocVien_MonHoc_DuocDKServiceImpl implements Tbl_DKMH_HocVien_MonHoc_DuocDKService,Serializable {

   private Log log = LogFactory.getLog(Tbl_DKMH_HocVien_MonHoc_DuocDKServiceImpl.class);
   private static final long serialVersionUID = -8740095022264495229L;

   @Autowired
   @Qualifier("tbl_DKMH_HocVien_MonHoc_DuocDKDAO")
   private Tbl_DKMH_HocVien_MonHoc_DuocDKDAO _tbl_DKMH_hocVien_MonHoc_DuocDKDao;

   @Autowired
   @Qualifier("tbl_DKMH_DotDK_HocVienDAO")
   private Tbl_DKMH_DotDK_HocVienDAO _tbl_DKMH_DotDK_HocVienDao;
   
   @Autowired
	@Qualifier("tbl_ThongTin_HocVienDAO")
	private Tbl_ThongTin_HocVienDAO _tbl_ThongTin_HocVienDao;

   @Autowired
   @Qualifier("tbl_CTDT_MonHocDAO")
   private Tbl_CTDT_MonHocDAO _tbl_CTDT_MonHocDao;

   public final List<Tbl_DKMH_HocVien_MonHoc_DuocDK> findAlltbl_DKMH_hocVien_MonHoc_DuocDK(int pageSize, int page){
   List<Tbl_DKMH_HocVien_MonHoc_DuocDK> result = get_tbl_DKMH_hocVien_MonHoc_DuocDKDao().findAll(pageSize, page);
       log.info("Number of user in DB:: "+result.size());
       return result;
   }

   @Transactional(noRollbackFor=IllegalArgumentException.class)
   public final Long createtbl_DKMH_hocVien_MonHoc_DuocDK(Tbl_DKMH_HocVien_MonHoc_DuocDK obj){
       return _tbl_DKMH_hocVien_MonHoc_DuocDKDao.save(obj);
   }

   public final void updatetbl_DKMH_hocVien_MonHoc_DuocDK(Tbl_DKMH_HocVien_MonHoc_DuocDK obj){
       _tbl_DKMH_hocVien_MonHoc_DuocDKDao.update(obj);
   }//end update methode

   public final Tbl_DKMH_HocVien_MonHoc_DuocDK gettbl_DKMH_hocVien_MonHoc_DuocDK (Long id){
       log.info("Try to retrieve the tbl_DKMH_hocVien_MonHoc_DuocDK: "+id);
       return get_tbl_DKMH_hocVien_MonHoc_DuocDKDao().get(id);
   }

   public void deletetbl_DKMH_hocVien_MonHoc_DuocDK(Long id){
       log.info("Try to delete the tbl_DKMH_hocVien_MonHoc_DuocDK by Id : "+ id);
       if(_tbl_DKMH_hocVien_MonHoc_DuocDKDao.exists(id)){
	    _tbl_DKMH_hocVien_MonHoc_DuocDKDao.delete(id);
       } else {
       	throw new IllegalArgumentException("EmployeeId '"+id+"' does not exists");
       }
   }

   public void deleteListtbl_DKMH_hocVien_MonHoc_DuocDK(String[] listId){
       for(String id : listId){
   	    deletetbl_DKMH_hocVien_MonHoc_DuocDK(Long.parseLong(id));
       }
   }

   public List<Tbl_DKMH_HocVien_MonHoc_DuocDK> findtbl_DKMH_hocVien_MonHoc_DuocDKByCondition(List<ConditionReport> cons,int pageSize,int page){ 
       return _tbl_DKMH_hocVien_MonHoc_DuocDKDao.findByCondition(Tbl_DKMH_HocVien_MonHoc_DuocDK.class, cons, pageSize, page); 
   } 
 
   public int counttbl_DKMH_hocVien_MonHoc_DuocDKByCondition(List<ConditionReport> cons){ 
       return _tbl_DKMH_hocVien_MonHoc_DuocDKDao.countByCondition(Tbl_DKMH_HocVien_MonHoc_DuocDK.class, cons); 
   } 
 
public List<Object> findColumntbl_DKMH_hocVien_MonHoc_DuocDKByCondition( 
       List<ConditionReport> cons, List<String> columns, int pageSize, int page) { 
   return _tbl_DKMH_hocVien_MonHoc_DuocDKDao.findColumnByCondition( 
		Tbl_DKMH_HocVien_MonHoc_DuocDK.class, cons, columns, pageSize, page); 
} 
   public final Tbl_DKMH_HocVien_MonHoc_DuocDKDAO get_tbl_DKMH_hocVien_MonHoc_DuocDKDao() {
   	return _tbl_DKMH_hocVien_MonHoc_DuocDKDao;
   }
   public final void set_tbl_DKMH_hocVien_MonHoc_DuocDKDao(Tbl_DKMH_HocVien_MonHoc_DuocDKDAO obj) {
   	this._tbl_DKMH_hocVien_MonHoc_DuocDKDao = obj;
   }

   public Tbl_DKMH_DotDK_HocVienDAO get_tbl_DKMH_DotDK_HocVienDao() {
       return _tbl_DKMH_DotDK_HocVienDao;
   }
   public void set_tbl_DKMH_DotDK_HocVienDao(Tbl_DKMH_DotDK_HocVienDAO _tbl_DKMH_DotDK_HocVienDao) {
       this._tbl_DKMH_DotDK_HocVienDao = _tbl_DKMH_DotDK_HocVienDao;
   }

   public Tbl_CTDT_MonHocDAO get_tbl_CTDT_MonHocDao() {
       return _tbl_CTDT_MonHocDao;
   }
   public void set_tbl_CTDT_MonHocDao(Tbl_CTDT_MonHocDAO _tbl_CTDT_MonHocDao) {
       this._tbl_CTDT_MonHocDao = _tbl_CTDT_MonHocDao;
   }

   public void createListtbl_DKMH_HocVien_MonHoc_DuocDKByJSON(String data) {
		try {
			ChumanoLogger.init(Tbl_DKMH_HocVien_MonHoc_DuocDKServiceImpl.class.getName());
			ChumanoLogger.log("0BAT DAU INSERT DU LIEU");
			List<Tbl_DKMH_HocVien_MonHoc_DuocDK> tbl_DKMH_HocVien_MonHoc_DuocDKs = new ArrayList<Tbl_DKMH_HocVien_MonHoc_DuocDK>();

			JSONArray jsons = (JSONArray) JSONSerializer.toJSON(data);
			ArrayList<String> notMonHocs = new ArrayList<String>();
			
			for (int i = 0; i < jsons.size(); i++) {
				JSONObject obj = jsons.getJSONObject(i);
				String logStr ="";
				//-------------------------------------
				//Lay du lieu
				//MSSV	MAMONHOC	DUOCDK
				String mssv = obj.getString("MSSV");
				String maMH = obj.getString("MAMONHOC");
				String duocDK = obj.getString("DUOCDK");
				
				Boolean isDK = true;
				if(duocDK==null || duocDK.isEmpty()){
					isDK= false;
				}
				
				logStr ="[ " + (i+1)+ " - " 
				+ "MSSV: " + mssv + " - " 
				+ "MAMH: " + maMH + " - " 
				+ "DUOCDK: " + duocDK 
				+ "]";
				//=====================================
				// kiem tra mshv co ton tai hay khong
				List<ConditionReport> cons = new ArrayList<ConditionReport>();
				{
					ConditionReport con = new ConditionReport();
					con.setCol("hocVienId.MSSV");
					con.setValue1(mssv);
					con.setOperator("eq");
					cons.add(con);
				}
				{

					ConditionReport con = new ConditionReport();
					con.setCol("monHocId.ma");
					con.setValue1(maMH);
					con.setOperator("eq");
					cons.add(con);
				}
				List<Tbl_DKMH_HocVien_MonHoc_DuocDK> thongTinHocVienMonHoc = _tbl_DKMH_hocVien_MonHoc_DuocDKDao.findByCondition(Tbl_DKMH_HocVien_MonHoc_DuocDK.class,cons, 1, 1);

				// neu ket qua da co=> cap nhat ket qua
				if (thongTinHocVienMonHoc != null&& thongTinHocVienMonHoc.size() > 0) {
					thongTinHocVienMonHoc.get(0).setDuocYN(isDK);
					// _tbl_ThongTin_HocVien_MonHocDao.saveOrUpdate(tblThongTinHocVienMonHoc);

					tbl_DKMH_HocVien_MonHoc_DuocDKs.add(thongTinHocVienMonHoc.get(0));

				}
				// neu chua co ket qua => insert
				else {
					Tbl_DKMH_HocVien_MonHoc_DuocDK tblThongTinHocVienMonHoc = new Tbl_DKMH_HocVien_MonHoc_DuocDK();

					cons.clear();
					{
						ConditionReport con = new ConditionReport();
						con.setCol("MSSV");
						con.setValue1(mssv);
						con.setOperator("eq");
						cons.add(con);
					}
					List<String> columns = new ArrayList<String>();
					columns.add("id");
					List<Long> hocVienId = _tbl_ThongTin_HocVienDao.findColumnByCondition(Tbl_ThongTin_HocVien.class,cons, columns, 1, 1);

					cons.clear();
					{
						ConditionReport con = new ConditionReport();
						con.setCol("ma");
						con.setValue1(maMH);
						con.setOperator("eq");
						cons.add(con);
					}
					List<Long> monHocId = _tbl_CTDT_MonHocDao.findColumnByCondition(Tbl_CTDT_MonHoc.class, cons,columns, 1, 1);

					// neu ton tai hoc vien co mssv va ton tai mon hoc co ma mon
					// hoc thi insert
					if (hocVienId != null && hocVienId.size() > 0 ) {
						if(monHocId != null && monHocId.size() > 0){
							Tbl_ThongTin_HocVien hocVien = new Tbl_ThongTin_HocVien();
							hocVien.setId(hocVienId.get(0));
							tblThongTinHocVienMonHoc.setHocVienId(hocVien);
	
							Tbl_CTDT_MonHoc monHoc = new Tbl_CTDT_MonHoc();
							monHoc.setId(monHocId.get(0));
							tblThongTinHocVienMonHoc.setMonHocId(monHoc);
							tblThongTinHocVienMonHoc.setDuocYN(isDK);
							//if (trangthai.trim().equals("1"))
								//tblThongTinHocVienMonHoc.setTrangThai("1");
							//else
								//tblThongTinHocVienMonHoc.setTrangThai("0");
	
							tbl_DKMH_HocVien_MonHoc_DuocDKs.add(tblThongTinHocVienMonHoc);
						} else{
							logStr +=" NOT MAMH";
							if(!notMonHocs.contains(maMH)) notMonHocs.add(maMH);
						}
					}
					else
						logStr +=" NOT MSSV";
				}// if
				
				
				ChumanoLogger.log(logStr);
			}// end for

			_tbl_DKMH_hocVien_MonHoc_DuocDKDao.saveOrUpdateAll(tbl_DKMH_HocVien_MonHoc_DuocDKs);
			//-------------------------
			
			ChumanoLogger.log("===================== not monhoc=======================");
			reportArrayString(notMonHocs);
			
		} catch (Exception e) {
			ChumanoLogger.log("ERROR: " +e.getMessage());
			   System.out.println("------------ERROR CHUMANO-------------");
			   e.printStackTrace();
			   System.out.println("=================================");
		}
		   
		ChumanoLogger.stop();
	}
	
	static void reportArrayString(ArrayList<String> arr){
		int i =1;
		for(String str :arr){
			ChumanoLogger.log(i + ". " + str);
			i++;
		}
	}
}//endclass

